home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / KEYBOARD.SWG / 0084_CTRL-BREAK-C disabling.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  3KB  |  90 lines

  1. (*
  2. :Could someone please tell me how to disable CTRL-BREAK/C in my program so the
  3. :user cannot exit without using my "exit" option?  The DOS BREAK=OFF just
  4.  
  5. The mother of all TP FAQs :-)
  6.  
  7. -From: garbo.uwasa.fi:/pc/ts/tsfaqp20.zip Frequently Asked TP Questions
  8. -Subject: Disabling or capturing the break key
  9.  
  10. 1. *****
  11.  Q: I don't want the Break key to be able to interrupt my TP
  12. programs. How is this done?
  13.  Q2: I want to be able to capture the Break key in my TP program.
  14. How is this done?
  15.  Q3: How do I detect if a certain key has been pressed?
  16.  
  17.  A: This very frequently asked question is basically a case of RTFM
  18. (read the f*ing manual). But this feature is, admittedly, not very
  19. prominently displayed in the Turbo Pascal reference. (As a general
  20. rule we should not use the newsgroups as a replacement for our
  21. possibly missing manuals, but enough of this line.)
  22.    There is a CheckBreak variable in the Crt unit, which is true by
  23. default. To turn it off use
  24.      uses Crt;
  25.      :
  26.      CheckBreak := false;
  27.      :
  28. Besides turning off break checking this enables you to capture the
  29. pressing of the break key as you would capture pressing ctrl-c. In
  30. other words you can use e.g.
  31.      :
  32. procedure TEST;
  33. var key : char;
  34. begin
  35.   repeat
  36.     if KeyPressed then
  37.       begin
  38.         key := ReadKey;
  39.         case key of
  40.           #3 : begin writeln ('Break'); exit; end;  {ctrl-c or break}
  41.           else write (ord(key), ' ');
  42.         end; {case}
  43.       end; {if}
  44.   until false;
  45. end;
  46.      :
  47. IMPORTANT: Don't test the ctrl-break feature just from within the TP
  48. IDE, because it has ctlr-break handler ("intercepter") of its own
  49. and may confuse you into thinking that ctrl-break cannot be
  50. circumvented by the method given above.
  51.   The above example has a double purpose. It also shows the
  52. rudiments how you can detect if a certain key has been pressed. This
  53. enables you to give input without echoing it to the screen, which is
  54. a later FAQ in this collection.
  55.   This is, however, not all there can be to break checking, since
  56. the capturing is possible only at input time. It is also possible to
  57. write a break handler to interrupt a TP program at any time. For
  58. more details see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  59. Chapter 7. (For the bibliography, see FAQPASB.TXT in this same FAQ
  60. collection).
  61.  
  62.  A2: Here is an example code for disabling Ctrl-Break and Ctrl-C
  63. with interrupts
  64. *)
  65.   uses Dos;
  66.   var OldIntr1B : pointer;  { Ctrl-Break address }
  67.       OldIntr23 : pointer;  { Ctrl-C interrupt handler }
  68.       answer    : string;   { For readln test }
  69.   {$F+}
  70.   procedure NewIntr1B (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  71.             Interrupt;
  72.   {$F-} begin end;
  73.   {$F+}
  74.   procedure NewIntr23 (flags,cs,ip,ax,bx,cx,dx,si,di,ds,es,bp : word);
  75.             Interrupt;
  76.   {$F-} begin end;
  77.   begin
  78.     GetIntVec ($1B, OldIntr1B);
  79.     SetIntVec ($1B, @NewIntr1B);   { Disable Ctrl-Break }
  80.     GetIntVec ($23, OldIntr23);
  81.     SetIntVec ($23, @NewIntr23);   { Disable Ctrl-C }
  82.     writeln ('Try breaking, disabled');
  83.     readln (answer);
  84.     SetIntVec ($1B, OldIntr1B);    { Enable Ctrl-Break }
  85.     SetIntVec ($23, OldIntr23);    { Enable Ctrl-C }
  86.     writeln ('Try breaking, enabled');
  87.     readln (answer);
  88.     writeln ('Done');
  89.   end.
  90.